home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / unixutil / trunc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  173 lines

  1. /* trunc.c - Remove trailing white space from a file.  In addition    *
  2.  *    perform some simple formatting.  Options include lm (left    *
  3.  *    margin), lc (leftmost column to copy), rc (rightmost column    *
  4.  *    to copy), mc (convert CR's to LF's), and md (delete CR's).      *
  5.  *                                    *
  6.  * trunc [-<mc>|<md>][-lm n][-lc n][-rc n] [<file> ...]         *
  7.  *                                    *
  8.  * trunc (C) 1988 by Gary L. Brant                    *
  9.  *                                    *
  10.  * :ts=8                                */
  11.  
  12. #define MAXLINE 1000
  13. #include <stdio.h>
  14. int lc = 0,        /* leftmost column to copy */
  15.     lm = 0,        /* left margin to insert */
  16.     rc = 0;        /* rightmost column to copy */
  17. int m  = 0;        /* conversion flag for ^M's */
  18. int flag = 0;        /* indicates CR seen and conversion mode on */
  19. int head = 0;
  20. void fputs (), putc ();
  21.  
  22. main (argc, argv)    /*remove trailing blanks and tabs from source lines */
  23. int argc;
  24. char *argv[];
  25.  
  26. {
  27.    int i = 0, j;
  28.    char ch, *pch;
  29.    FILE *ifile, *fopen ();
  30.  
  31.    while ((++i < argc) && (argv[i][0] == '-')) {
  32.       j = 1;
  33.       switch (ch = argv[i][j++]) {
  34.       case 'l': if ((ch = argv[i][j]) == 'c' && i < argc - 1) {
  35.            lc = convert (argv[++i]);
  36.            if (lc > 0)
  37.               lc--;
  38.         } else if (ch = 'm' && i < argc - 1)
  39.            lm = convert (argv[++i]);
  40.         else
  41.            badarg ();
  42.         break;
  43.       case 'm': if ((ch = argv[i][j]) == 'd')
  44.            m = 1;
  45.         else if (ch == 'c')
  46.            m = 2;
  47.         break;
  48.       case 'r': if ((ch = argv[i][j]) == 'c' && i < argc - 1)
  49.            rc = convert (argv[++i]);
  50.         else
  51.            badarg ();
  52.         break;
  53.       default:    badarg ();
  54.         break;
  55.       }
  56.    }
  57.    if (rc == 0)                 rc = MAXLINE - 1 - lm;
  58.    if (lc > rc || rc - lc + lm >= MAXLINE)    badarg ();
  59.  
  60.    while (i < argc) {
  61.       ++head;
  62.       if ((ifile = fopen ((pch = argv[i++]), "r")) == NULL) {
  63.      fputs ("trunc: cant open ", stderr);
  64.      fputs (pch, stderr);
  65.      putc ('\n', stderr);
  66.      exit (20);
  67.       } else
  68.      copy (ifile);
  69.    }
  70.    if (head == 0)
  71.       copy (stdin);
  72. }
  73.  
  74.  
  75. /* copy - copy file, remove trailing white space and format as we go    */
  76. /* inspired by example in: K & R, P. 61                 */
  77.  
  78. copy (ifile)
  79. FILE *ifile;
  80. {
  81.    int irc, n = 0;
  82.    char line[MAXLINE];
  83.  
  84.    irc = rc + lm - lc;
  85.    while (n < lm)
  86.       line[n++] = ' ';
  87.    while ((n = getline (ifile, &line[lm], MAXLINE-1-lm)) > 0) {
  88.       register int in = (lm + n < irc) ? lm + n : irc;
  89.       while (in-- >= lm)
  90.      if (line[in] != ' ' && line[in] != '\t' && line[in] != '\n')
  91.         break;
  92.       if (in < lm) {
  93.      putc ('\n', stdout);
  94.      if (flag) {
  95.         flag = 0;
  96.         putc ('\n', stdout);
  97.      }
  98.       } else {
  99.      if (flag != 0) {
  100.         line[++in] = '\n';
  101.         flag = 0;
  102.      }
  103.      line[++in] = '\n';
  104.      line[++in] = '\0';
  105.      fputs (line, stdout);
  106.       }
  107.    }
  108. }
  109.  
  110.  
  111. /* get line into s, return length
  112.  * inspired by example in: K & R, P. 67     */
  113.  
  114. getline (ifile, s, lim)
  115. FILE *ifile;
  116. char s[];
  117. int  lim;
  118. {
  119.    register int c, i, j;
  120.  
  121.    i = 0;    j = lc;     /* leftmost columns to ignore */
  122.    while (i < lim && (c = getc (ifile)) != EOF && c != '\n') {
  123.       if (c == '\r') {
  124.      if (m == 1)
  125.         continue;
  126.      else if (m == 2) {
  127.         flag = 1;
  128.         continue;
  129.      }
  130.       }
  131.       if (j-- <= 0)
  132.      s[i++] = c;
  133.    }
  134.    if (c  == '\n')
  135.       s[i++] = c;
  136.    s[i] = '\0';
  137.    return (i);
  138. }
  139.  
  140.  
  141. /* convert - convert numeric command-line arguments to binary    *
  142.  * returns -1 if non-numeric data encountered            */
  143.  
  144. convert (argv)
  145. char *argv;
  146. {
  147.    register long i = 0;
  148.    register char ch;
  149.    register int j=0;
  150.  
  151.    while ((ch = argv[j++]) != '\0')
  152.       if (ch >= '0' && ch <= '9') {
  153.      i *= 10;
  154.      i += ch - '0';
  155.       } else
  156.      return (-1);
  157.    return (i);
  158. }
  159.  
  160.  
  161. /* badarg - complain about bad argument */
  162.  
  163. badarg ()
  164. {
  165.      fputs ("bad args\n", stderr);
  166.      exit (20);
  167. }
  168.  
  169.  
  170. _wb_parse ()
  171. {
  172. }
  173.